package com.gtxc.kotlinmvvm.ui.fragment

import android.os.Bundle
import android.util.Log
import android.view.Gravity
import android.widget.Toast
import com.aleyn.mvvm.base.BaseFragment
import com.blankj.utilcode.util.ToastUtils
import com.gtxc.kotlinmvvm.BR
import com.gtxc.kotlinmvvm.R
import com.gtxc.kotlinmvvm.databinding.FrgamentRegisterBinding
import com.gtxc.kotlinmvvm.network.request.RegisterBean
import com.gtxc.kotlinmvvm.network.request.RequestTestBean
import com.gtxc.kotlinmvvm.utlis.CountDownSmsUtil
import com.gtxc.kotlinmvvm.utlis.setOnclickNoRepeat
import com.gtxc.kotlinmvvm.viewmodle.RegisterViewModle

class RegisterFragment : BaseFragment<RegisterViewModle, FrgamentRegisterBinding>() {
    private var countDown: CountDownSmsUtil? = null

    override fun initView(savedInstanceState: Bundle?) {

    }
    override fun lazyLoadData() {
        super.lazyLoadData()
        viewModel.userName.observe(this, {
            Log.e("TAccccG", "onChanged: 数据有更新" + it)
            mBinding?.ed04?.setText(it)
        })

        viewModel.password1.observe(this, {
            Log.e("TAccccG", "onChanged: 数据有更新" + it)
            mBinding?.ed06?.setText(it)
        })
        viewModel.invitationCode.observe(this, {
            Log.e("TAccccG", "onChanged: 数据有更新" + it)
            mBinding?.ed4?.setText(it)
        })
        viewModel.password2.observe(this, {
            Log.e("TAccccG", "onChanged: 数据有更新" + it)
            mBinding?.ed9?.setText(it)
        })
        gettime();
    }

    override fun onBindViewClick() {
//        防止重读点击事件
        setOnclickNoRepeat(mBinding?.tv07, mBinding?.xiayibuBut){
            when (it.id) {
//                发送验证码
                R.id.tv_07 -> {
                    when {
                        mBinding?.ed04?.text.toString().isEmpty() -> ToastUtils.showLong("手机号不能为空")
                        else ->{
                            countDown?.start()
                            viewModel.getCode(RequestTestBean(mBinding?.ed04?.text.toString(), "register"))
                        }
                    }
                }
//                注册
                R.id.xiayibu_but -> {
//                    toStartActivity(LoginActivity::class.java)
                    when {
                        mBinding?.ed04?.text.toString().isEmpty() -> ToastUtils.showLong("手机号不能为空")
                        mBinding?.ed06?.text.toString().isEmpty() -> ToastUtils.showLong("验证码不能为空")
//                        邀请码没做判断
                        mBinding?.ed4?.text.toString().isEmpty() -> ToastUtils.showLong("邀请码不能为空")
                        mBinding?.ed5?.text.toString().isEmpty() -> ToastUtils.showLong("密码不能为空")
                        mBinding?.ed9?.text.toString().isEmpty() -> ToastUtils.showLong("请确认密码")
                        else -> viewModel.getRegister(
                            RegisterBean(
                                mBinding?.ed9?.text.toString(),
                                mBinding?.ed4?.text.toString(),
                                mBinding?.ed04?.text.toString(),
                                mBinding?.ed5?.text.toString(),
                                mBinding?.ed06?.text.toString()
                            )
                        )
                    }

                }
            }

        }
    }

   fun gettime(){
       //        countDown = new CountDownSmsUtil(this, mSendMsmTv, "%s秒");
       countDown = CountDownSmsUtil(activity, mBinding?.tv07, "%s秒", 60)
//        countDown.start();
       //        countDown.start();
       countDown!!.setCountdownListener(object : CountDownSmsUtil.CountdownListener {
           override fun onStart() {

               ToastUtils.showLong("已发送短信")

           }

           override fun onFinish() {
               mBinding?.tv07?.setText("获取验证码")
           }

           override fun onUpdate(currentRemainingSeconds: Int) {
               Log.i("TAG", "onUpdate: "+currentRemainingSeconds)
               mBinding?.tv07?.setText(currentRemainingSeconds.toString())
           }


       })
   }
    override fun layoutId(): Int {
        return R.layout.frgament_register
    }

    override fun onDestroy() {
        super.onDestroy()
        countDown?.onDestroy()
    }
    override fun initVariableID(): Int {
        return BR.RegisterViewModle
    }
}